clear all

cd "~/Dropbox (Harvard University)/Monetary Policy with Heterogenous Producers/Replication Code"

ssc install grstyle, replace
grstyle init
grstyle set plain, nogrid
graph set window fontface "Palatino"

/*************** DESCRIPTION **********************************************
 Aggregate quarterly Compustat markup data for VARs
*************************************************************************/ 

use "data/Compustat/Compustat_clean", clear

// Remove Damadoran estimates before 1975 since they are very unclear
replace mu_cfg1_dam=. if year<1975
replace mu_cfg2_dam=. if year<1975

// 1. Annual labor share and aggregate markup
g saleq_pos = saleq 
replace saleq_pos = 0 if saleq < 0
bysort quarter: egen sale_total = total(saleq_pos)
gen sale_pct = saleq_pos / sale_total
local mu_list mu_li mu_li_ms4 mu_cfg1_dam mu_cfg2_dam mu_cfg1_ms4 mu_cfg2_ms4
foreach mu in `mu_list' {
	gen sale_pct_over_`mu' = sale_pct / `mu' 
	bysort quarter: egen labor_share_`mu' = total(sale_pct_over_`mu')
	gen `mu'_bar = 1/labor_share_`mu'
	gen log_`mu' = log(`mu')
}

save "data/Compustat/Compustat_clean_withmubar", replace
collapse (first) mu*bar, by(quarter)
save "data/markup_estimates/quarterly_mu_bar", replace

use "data/Compustat/Compustat_clean_withmubar", clear

// 2. Diff in sales growth low vs. high
local mu_list mu_li mu_li_ms4 mu_cfg1_dam mu_cfg2_dam mu_cfg1_ms4 mu_cfg2_ms4
foreach mu in `mu_list' {
	bysort quarter: egen median_`mu' = median(`mu')
	g above_median_`mu' = 0
	replace above_median_`mu' = 1 if `mu' > median_`mu'
	replace above_median_`mu' = . if `mu'==.
}
sort gvkey quarter
xtset gvkey quarter
forvalues t=-10/20 { 
	local str_t = `t'
	if `t' < 0 {
		local abs_t = -1 * `t' 
		local str_t = "m`abs_t'"
		disp "`str_t'"
	}
	g saleq_pos_`str_t' = .
	if `t' < 0 {
		replace saleq_pos_`str_t' = L`abs_t'.saleq_pos
	} 
	else {
		replace saleq_pos_`t' = F`t'.saleq_pos
	}
}

save "data/Compustat/Compustat_clean_withmediandata", replace

local mu_list mu_li mu_li_ms4 mu_cfg1_dam mu_cfg2_dam mu_cfg1_ms4 mu_cfg2_ms4
foreach mu in `mu_list' {
	use "data/Compustat/Compustat_clean_withmediandata", clear
	collapse (sum) saleq_pos*, by(quarter above_median_`mu')
	drop if above_median_`mu'==.
	forvalues t=-10/20 { 
		local str_t = `t'
		if `t' < 0 {
			local abs_t = -1 * `t' 
			local str_t = "m`abs_t'"
			disp "`str_t'"
		}
		gen sale_growth_`str_t' = log(saleq_pos_`str_t') - log(saleq_pos)
	}
	keep quarter above_median_`mu' sale_growth*
	rename (sale_growth*) =_
	reshape wide sale_growth*, i(quarter) j(above_median_`mu')
	forvalues t=-10/20 { 
		local str_t = `t'
		if `t' < 0 {
			local abs_t = -1 * `t' 
			local str_t = "m`abs_t'"
			disp "`str_t'"
		}
		gen diff_sale_g_`mu'_`str_t' = sale_growth_`str_t'_1 - sale_growth_`str_t'_0
	}
	keep quarter diff_sale_g*
	save "data/data_temp/quarterly_diff_sales_growth_`mu'", replace
}

use "data/Compustat/Compustat_clean_withmediandata", clear
local mu_list mu_li mu_li_ms4 mu_cfg1_dam mu_cfg2_dam mu_cfg1_ms4 mu_cfg2_ms4
foreach mu in `mu_list' {
	merge m:1 quarter using "data/data_temp/quarterly_diff_sales_growth_`mu'"
	drop if _merge==2
	drop _merge
}

collapse (first) mu*bar diff_sale_g*, by(quarter)
save "data/markup_estimates/aggregate_quarterly_mu", replace

* Move all markups (and sales) before/after by lag
use "data/Compustat/Compustat_clean_withmediandata", clear
keep gvkey quarter naics* sic* ind* saleq sale_ms4 sale_ms4_win mu_li_ms4 mu_cfg1_ms4 mu_cfg2_ms4
local mu_list saleq sale_ms4_win mu_li_ms4 mu_cfg1_ms4 mu_cfg2_ms4
foreach mu in `mu_list' {
	forvalues t=-5/18 { 
		local str_t = `t'
		if `t' < 0 {
			local abs_t = -1 * `t' 
			local str_t = "m`abs_t'"
			disp "`str_t'"
		}
		if `t' < 0 {
			g `mu'_lag_`str_t' = L`abs_t'.`mu'
		} 
		else {
			g `mu'_lag_`str_t' = F`t'.`mu'
		}
		g log_`mu'_`str_t' = log(`mu'_lag_`str_t')
		g dlog_`mu'_`str_t' = log(`mu'_lag_`str_t') - log(`mu')
	}
}

save "data/Compustat/Compustat_clean_with_lags", replace
